<?php

/**
 * Class ProductsController
 */
class ProductsController extends AppController {

	/**
	 * Model
	 *
	 * @type array
	 */
	public $uses = ['Product'];

	/**
	 * Component
	 *
	 * @type array
	 */
	public $components = ['Paginator'];

	/**
	 * Helper
	 *
	 * @type array
	 */
	public $helpers = ['Form', 'Paginator'];

	/**
	 * Initialization
	 */
	public function beforeFilter() {
		parent::beforeFilter();
	}

	/**
	 * Index product
	 */
	public function index() {
//		$this->Product->getCreateData();
//		die('test');
		$this->Paginator->settings = [
			'Product' => [
				'paramType' => 'querystring',
				'recursive' => -1,
				'limit' => PHP_INT_MAX,
				'order' => [
					'Product.created' => 'desc'
				],
			]
		];
		$this->set('products', $this->Paginator->paginate());
		$this->set('topTitle', '产品列表');
	}

	/**
	 * Add product
	 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Product->create();
			if ($this->Product->save($this->request->data)) {
				$this->success("产品 `{$this->request->data['Product']['name']}` 添加成功");
			}
			$this->success("产品 [{$this->request->data['Product']['name']}] 添加失败");
		}
		$this->set('topTitle', '添加产品');
	}

	/**
	 * Edit product
	 */
	public function edit($id = null) {
		$product = $this->Product->findById($id);
		if (!$product) {
			throw new NotFoundException(__('Product not found'));
		}
		if ($this->request->is('ajax')) {
			$this->Product->id = $id;
			if ($this->Product->save($this->request->data)) {
				$this->success("产品 [{$this->request->data['Product']['name']}] 更新成功");
			}
			$this->success("产品 [{$this->request->data['Product']['name']}] 更新失败");
		} else {
			$this->request->data = $product;
		}
		$this->set('topTitle', '编辑产品');
	}

	/**
	 * Delete product
	 */
	public function delete($id) {
		$this->isAjax();
		if ($this->Product->delete($id)) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}

	/**
	 * View product
	 */
	public function view($id = null) {
		$this->loadFrame();
		$product = $this->Product->findById($id);
		$this->set(compact('product'));
	}

	/**
	 * Search product
	 *
	 * This method first detects a POST request and then does a super-quick implementation of the Post/Redirect/Get
	 * design pattern. We switched the posted searchQuery request data (sent using the input field in our search form)
	 * to a query string parameter and then issued a redirect to the same action, appending the new search query to the
	 * URL
	 *
	 * For example, if we fill the search box with the example text and then hit the Submit button,
	 * we'd post the form data to the search() action first and then issue a redirect to /products/index?q=example.
	 * Using this pattern will provide a better search experience for users and also remember the query variable
	 * in all of the pagination and order columns of our page
	 */
	public function search() {
		if ($this->request->is('put') || $this->request->is('post')) {
			return $this->redirect(array(
				'?' => array(
					'q' => $this->request->data('Product.searchQuery')
				)
			));
		}
		$this->Product->recursive = 0;
		$searchQuery = $this->request->query('q');
		$this->Paginator->settings = [
			'Product' => [
				'findType' => 'search',
				'searchQuery' => $searchQuery,
				'limit' => PHP_INT_MAX,
				'order' => [
					'Product.created' => 'desc'
				],
			]
		];
		$this->set('products', $this->Paginator->paginate());
		$this->set('searchQuery', $searchQuery);
		$this->render('index');
	}
}